/*
 * DA5b.c
 *
 * Created: 4/20/2015 5:50:00 PM
 *  Author: David Hunt
 */ 

//STEPPER MOTOR
#include <avr/io.h>

int main(void)
{	
	ADCSRA = 0x87;		//ENABLE ADC AND PRESCALE CK/128
	ADMUX = 0xC0;		//2.56V VREF, ADC0 SINGLE ENDED INPUT, DATA RIGHT JUSTIFIED
	DDRC = 0;			//SET PORTC AS INPUT FOR ADC
	DDRB = 0xFF;		//SET PORTB AS OUTPUT
	
    while(1)
    {
		ADCSRA |= (1<<ADSC);	//STARTING CONVERSION
		while((ADCSRA&(1<<ADIF))==0); //WAIT FOR CONVERSION TO FINISH
		int X=ADCL;			//SET X BASED ON RESULT OF CONVERSION
		int I;				//VARIABLE TO COUNT FOR DELAY
		
		PORTB = 0x66;	//set step
		for(I=X;I>0;I=I-1);	//delay based on conversion
		PORTB = 0xCC;	//set step
		for(I=X;I>0;I=I-1);	//delay based on conversion
		PORTB = 0x99;	//set step
		for(I=X;I>0;I=I-1);	//delay based on conversion
		PORTB = 0x33;	//set step
		for(I=X;I>0;I=I-1);	//delay based on conversion
    }
}